<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--{include file='comm/comm_editor.tpl'}-->
    <title>添加下午茶采购单</title>
</head>
<body>



<div class="container">



    <div class="comm_crub">
        <span class="now_crub">添加下午茶采购单</span>
    </div>

    <div class="comm_content">

            <div  >
                <form id="searchForm" class="form-inline" onsubmit="return false;">
                    <div class="search-box" style="position:relative;background-color: whitesmoke;" >
                         <span>
                            <span style='width:120px;text-align:right'><span class="red">*</span>配送日期：</span>
                            <input type="text" class="form-control input-sm" style="width: 150px;" value="<!--{$currentDate}-->" name="apply_date" id="apply_date">
                        </span></br>
                        <span>
                            <span style='width:120px;text-align:right'><span class="red">*</span>配送城市：</span>
                            <select id="cityID" name="cityID" class="form-control input-sm" style="width:150px;">
                                <option value="">请选择</option>
                            </select>
                        </span></br>
                        <span>
                            <span style='width:120px;text-align:right'><span class="red">*</span>配送站：</span>
                            <select name="distribution_id" id="distribution_id" class="form-control input-sm" style="width:150px;">
                                <option value="">请选择</option>
                            </select>
                        </span></br>
                        <span>
                            <span style='width:120px;text-align:right'>备注：</span>
                            <textarea class="form-control w300" id="remark" name="remark" placeholder="请输入备注"></textarea>
                        </span></br>
                    </div>
                    <div class="panel panel-default">

                        <div class="panel-heading"><h3 class="panel-title">申请商品信息</h3></div>

                        <table class="table table-hover">
                            <thead>
                            <tr>
                                <th width="*">商品名称</th>
                                <th width="180" style="text-align: left;">商品编号</th>
                                <th width="100" style="text-align: left;">申请数量</th>
                                <th width="100" style="text-align: center;">操作</th>
                            </tr>
                            </thead>
                            <tbody id="general_line"></tbody>
                            <tbody>
                            <tr>
                                <td colspan="13" class="form-inline">
                                    <input type="text" class="form-control w300" id="goods_name" placeholder="商品名称"/>
                                    <span class="red">填写商品名称按回车键即可选择商品</span>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </form>
                <div class="bottom_fixbox">
        			<div class="tac" style="background: #dddddd;">
           			<input type="button" class="btn btn-primary" onclick="coupon.add();" value="保存" />&nbsp;&nbsp;&nbsp;
            		<input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
        		</div>
            </div>
        </div>
            <input type="hidden" id="cityName"/>
            <input type="hidden" id="distribution_name"/>
        </div>


</div>

<script type="text/javascript">

    $('#apply_date').datepicker();

    var coupon = {
        choose_data: { },
        general_goods:{ },

        add : function(){

        		var apply_date = $('#apply_date').val();
        		var cityID = $('#cityID').val();
        		var distribution_id = $('#distribution_id').val();
        		var cityName = $('#cityName').val();
        		var distribution_name = $('#distribution_name').val();
                var remark=$("#remark").val();
                var len=0;


                //获取普通商品的信息
                $.each($("#general_line tr"),function(k,v){
                    var key = $("#general_line tr").eq(k).attr('id').replace('goods_','');
                    coupon.general_goods[key] = {
                        product_code : $("#product_code_"+key).html(),
                        product_name : $("#goodsName_"+key).html(),
                        apply_amount : $("#amount_"+key).html()
                    }
                    len++;
                });

        		var msg ='';
        		if(apply_date==''){
        			msg+="请填写配送日期<br />";

        		}
        		if( cityID=='' ){
        			msg+="请选择配送城市<br />";
        		}
				if(distribution_id==''){
					msg+="请选择配送站<br />";
				}
                if(len<1){
                    msg+="请添加申请商品<br />";
                }

        		if(msg !=''){
        			E.alert(msg);
        			return false;
        		}
                E.ajax_post({
		            url: "/invokingAjax-backend/production/applySearch.ajax?operFlg=2",
		            data: {
                        apply_date : apply_date ,
                        cityID : cityID,
                        distribution_id : distribution_id,
                        cityName : cityName,
                        distribution_name : distribution_name,
                        remark : remark,
                        general_goods:coupon.general_goods
		            },
                    call: function(data){
						if(data.code=200){

							E.alert('添加采购单成功',2);
                            E.opener('coupon.search');
                            location.reload();
						}else{
                            E.alert(data.message);
                        }

		            }
		        });
        },
        search:function( product_code, goods_name ) {

            this.product_code = product_code;
            this.goods_name = goods_name;

            E.loadding.open("正在加载商品，请稍等...");
            E.ajax_get({
                url: '/invokingAjax-comm/bill/checkoutGoods.ajax?operFlg=2',
                data: {
                    product_code: product_code,
                    goods_name: goods_name,
                    cityID:$("#cityID").val(),
                    pcustID: 15221472032,
                    source: "web"
                },
                call: "coupon.result"
            });
        },
        result: function( o ) {

            E.loadding.close();
            if (o.code == 200) {

                $('#goods_code').val('');
                $('#goods_name').val('');

                var html = '<div class="clearfix">';
                $.each(o.data, function(k, v) {
                    html += '<div class="panel panel-default" style="width: 320px;float: left;margin: 5px;">';
                    html += '<div class="panel-heading">';
                    html += '<h3 class="panel-title">' + v.goodsName + '</h3>';
                    html += '</div>';

                    html += '<ul class="list-group radio-box">';
                    $.each(v.list, function(i, j) {
                        html += '<li class="list-group-item">';
                        html += '<input type="checkbox" id="goods-square-checkbox-' + j.postID + '" name="goods-checkbox">';
                        html += '<label for="goods-square-checkbox-' + j.postID + '">' + j.spec + '</label>';
                        html += '<input type="text" class="form-control input-sm" id="goods-amount-input-'+ j.postID +'" value="1" style="width: 50px;float: right;">';
                        html += '</li>';
                    });
                    html += '</ul>';

                    html += '</div>';
                });
                html += '</div>';

                E.popup.open({
                    title: '选择商品',
                    content: html,
                    css: 'width: 700px;',
                    btnName: '选择',
                    btnCall: "coupon.choose();"
                });

                var html = '<div class="clearfix">';
                $.each(o.data, function(k, v) {

                    $.each(v.list, function(i, j) {
                        $('#goods-square-checkbox-' + j.postID).iCheck({
                            checkboxClass: 'icheckbox_square-blue',
                            radioClass: 'iradio_square-blue',
                            increaseArea: '20%' // optional
                        });

                        $('#goods-square-checkbox-' + j.postID).on('ifChecked', function() {
                            coupon.choose_data[j.postID] = { };
                            coupon.choose_data[j.postID].product_code = j.product_code;
                            coupon.choose_data[j.postID].goodsName = j.goodsName;
                        });

                        $('#goods-square-checkbox-' + j.postID).on('ifUnchecked', function() {
                            delete coupon.choose_data[j.postID];
                        });
                    });
                });

            } else {
                E.alert(o.message);
            }
        },
        choose: function() {

            var product_code = '';
            $.each(coupon.choose_data, function(k, v) {

                var amount = $("#goods-amount-input-"+k).val();
                if(!E.isInt(amount)){
                    E.alert('商品数量必须是非负整数');
                    return;
                }

                coupon.choose_data[k].amount = amount;
                var goods_html = [
                    '<tr class="success" id="goods_'+ k +'">',
                    '<td><label id="goodsName_'+ k +'">' + v.goodsName + '</label></td>',
                    '<td><label id="product_code_'+ k +'">' + v.product_code + '</label></td>',
                    '<td><label id="amount_'+ k +'">'+amount+'</label></td>',
                    '<td class="tac"><a class="delete_goods" href="javascript: void(0);" ebsig-data="'+ k +',general">删除</a></td>',
                    '</tr>'
                ].join('');

                $('#general_line').append(goods_html);
            });

            coupon.choose_data = {};
            E.popup.close();
        }

    };

    $(window).ready(function() {

        $("#cityID").html("");

        var cityHtml = '<option value="">请选择</option>';

        $.ajax({
            type: "post",
            url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=1",
            dataType: "json",
            data: "",
            success: function( result_obj ) {
                if(result_obj.code == 200){
                    $.each(result_obj.data, function(i,val){
                        cityHtml +='<option value="'+val.id+'">'+val.name+'</option>';
                    });
                    $('#cityID').html(cityHtml);
                }else{
                    E.alert(result_obj.message);
                }
            }
        });

        //点击城市联动获得配送站(联动)
        $(document).on('change', '#cityID', function() {
            $('#distribution_id').html("");
            $('#cityName').val($('#cityID').find('option:selected').text());

            var cityID = $('#cityID').children('option:selected').val();
            var distributionHtml = '<option value="">请选择</option>';
            E.ajax_post({
                url: "/invokingAjax-comm/bill/distribution.ajax?operFlg=2",
                data: {deliver_cityid: cityID},
                call: function( obj ) {
                    if(obj.code ==200 && obj.data != null){

                        $.each(obj.data, function(i,val){
                            distributionHtml +='<option value="'+val.distribution_id+'">'+val.distribution_name+'</option>';
                        });
                        $('#distribution_id').html(distributionHtml);
                    }else{

                        distributionHtml += '';
                        $('#distribution_id').html(distributionHtml);
                    }
                }
            });
        });
        //点击配送站事件
        $(document).on('change', '#distribution_id', function() {
            $('#distribution_name').val($('#distribution_id').find('option:selected').text());
        });

        $('#goods_name').on({
            keypress: function(event) {

                if (event.keyCode == '13') {
                    event.preventDefault();
                    var goods_name = E.trim($(this).val());
                    if (E.isEmpty(goods_name)) {
                        E.alert('请输入商品名称');
                        return false;
                    }
                    var cityID = $('#cityID').val();
                    if (E.isEmpty(cityID)) {
                        E.alert('请选择配送城市');
                        return false;
                    }
                    coupon.search('', goods_name);
                }
            }
        });

        $(document).on('click', 'a.delete_goods', function() {
            var data = $(this).attr('ebsig-data').split(',');
            E.confirm('您确认要删除该商品吗？',function(){
                if (data[1] == 'general') {
                    $("#goods_"+data[0]).remove();
                    delete coupon.choose_data[data[0]];
                }
            });

        });

    });

</script>

</body>
</html>